package com.huida.app.nullvalue.avoidNPE;

import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wangzhuangzhuang
 * @DESC:
 * @date 2022-01-03 14:51
 */
@Data
@Slf4j
public class AvoidNpe {


	public static void main(String[] args) throws Exception {
		AvoidNpe avoidNpe = new AvoidNpe();
		Student wangzz = new Student("wangzz", "22");
		Student stu = null;
//		avoidNpe.test(wangzz);
		avoidNpe.execute(stu);
	}

	private void execute(Student student) throws Exception{
		Optional.ofNullable(student)
				.orElseThrow(() -> new Exception("取值错误！"));
	}

	private void test(Student stu) throws Exception {
		Optional.ofNullable(stu).ifPresent(s -> {
			printInfo(s.getName(), s.getAge());
		});
	}

	private void printInfo(String stuName, String stuAge) {
		log.info("Student's name: {} age: {}", stuName, stuAge);
	}
}

@Data
@Slf4j
@AllArgsConstructor
class Student {
	private String name;
	private String age;
}